var partnerlist = {
		
		curPage : 1,
		pageSize : 10,
		
		type:1,//1：省，2：市，3：区
		managerType:[null,10,11,12],
		roleList : [],
		initPage : function(){
			partnerlist.initBtn();
			partnerlist.getDatalist(0);
			partnerlist.getRoleList();
		},
		
		initBtn : function(){
			$("#addBtn").click(function(){
				partnerlist.addUser();
			});
			$("#qryBtn").click(function(){
				partnerlist.getDatalist(0);
			});
		},
		
		getDatalist : function(pageIndex){
			$('#partnerlist').html('<tr><td  colspan="10"  style="text-align:center;">正在查询,请稍后...</td></tr>');
			$('#partnerlistpage').html("");
			pageIndex=(pageIndex==null)? 0:pageIndex;
			var params = {};
			params['curPage'] = pageIndex + 1;
			params['pageSize'] = partnerlist.pageSize;
			params['type'] = partnerlist.type;
			params['partnername'] = $("#partnername").val();
			partnermanager.queryList(params, partnerlist.getpartnerlist_Succ, partnerlist.getpartnerlist_error);
			partnerlist.curPage = pageIndex + 1;
		},
		
		getpartnerlist_Succ : function(data){
			if(data && data.retCode == 0){
				var _html = '';
				if(data.data && data.data.length > 0){
					for(var key in data.data){
						var item = data.data[key];
						if(item.userType=='1'){
							continue;
						}
						if(item.versionSize==null){
							item.versionSize=0;
						}
						_html += '<tr><td class="tal"> '+((parseInt(partnerlist.curPage)-1)*partnerlist.pageSize + (parseInt(key)+1))+'</td>';
						_html += '<td class="tal">'+item.cityname+'</td>';
						_html += '<td class="tal">'+item.partnername+'</td>';
						_html += '<td class="tal">'+item.remark+'</td>';
						_html += '<td class="tal">'+item.updatetime+'</td>';
//						if(item.account=='admin'){
//							_html += '<td></td></tr>';
//						}else{
							_html += '<td> ';
//							_html += '<button class="btn btn-info radius" onclick=\"javascript:partnerlist.addManager('+item.id+');\">添加管理员</button>';
							_html += ' <button class="btn btn-warning radius" onclick=\"javascript:partnerlist.editUser('+item.id+');\">编辑</button>';
//							_html += ' <button class="btn btn-danger radius" onclick="partnerlist.singleDel('+item.id+')">删除</button>';
							_html += '</td></tr>';
//						}
					}
					$('#partnerlist').html(_html);
					if(data.page){
						util.paginationPage($('#partnerlistpage'), data.page.totalPage, parseInt(data.page.curPage-1), partnerlist.getDatalist,true);
					}
				}else{
					$('#partnerlist').html('<tr  align="center"><td id="noData" colspan=10>暂无数据</td></tr>');
					$('#partnerlistpage').html("");
				}
			}else{
				layer.msg("查询错误");
//				$.dialog( {
//					title : '错误',
//					icon : 'error',
//					content : language.getError[mainIndex.languageType],
//					time : 3
//				});
			}
		},
		
		
		getpartnerlist_error:function(data){
			window.location.reload();
		},
		
		getRoleList : function(){
			partnermanager.queryRoleList({}, function(data){
				if(data && data.data){
					partnerlist.roleList = data.data;
				}
			});
		},
		
		addUser : function(){
			util.loadContent(util.webpath+'partner/addpartner.html','contentDiv',{},function(){
				addpartner.type = partnerlist.type;
				addpartner.initPage();
			});
		},

		editUser : function(id){
			util.loadContent(util.webpath+'partner/addpartner.html','contentDiv',{},function(){
				addpartner.id=id;
				addpartner.type = partnerlist.type;
				addpartner.initPage();
			});
		},
		
		addManager : function(id){
			var p = {};
			p['type']=partnerlist.managerType[partnerlist.type];
			p['partnerid']=id;
			partnermanager.queryManagerList(p, function(data){
				var username = "",roleid="",account="",managerid = "";
				if(data && data.data && data.data.length > 0){
					var obj = data.data[0];
					username = obj.username;
					roleid = obj.roleid;
					account=obj.account;
					managerid = obj.id;
				}
				var _html = '<table>';
				_html += '<tr>';
				_html += '<td>账号</td><td>';
				if(account){
					_html += account;
				}else{
					_html += '<input type="text" id="account" value="'+account+'" />';
				}
				_html += '</td></tr>';
				_html += '<tr>';
				_html += '<td>用户名</td><td>';
				_html += '<input id="username" value="'+username+'" />';
				_html += '</td></tr>';
				_html += '<tr>';
				_html += '<td>密码</td><td>';
				_html += '<input id="password" value="" />';
				_html += '</td></tr>';
				_html += '<tr>';
				_html += '<td>角色</td>';
				_html += '<td>';
				_html += '<select id="roleList">';
				for(var key in partnerlist.roleList){
					var o = partnerlist.roleList[key];
					_html += '<option value="'+o.id+'" '+(o.id==roleid?'selected':'')+' >'+o.name+'</option>';
				}
				_html += '</select>';
				_html += '</td>';
				_html += '</tr>';
				_html += '</table>';
				var p = {
						id:managerid
				};
				if(!p.id){
					p['type'] = partnerlist.managerType[partnerlist.type];
					p['partnerid'] = id;
				}
				layer.open({
					id:'setManager',
					title: '设置管理员',
					type:1,
					content: _html
					,btn: ['确认', '取消']
				  ,yes: function(index, layero){
				    //按钮【按钮一】的回调
					  p['account']=$("#account").val();
					  p['username']=$("#username").val();
					  p['password']=$("#password").val();
					  p['roleid']=$("#roleList").val();
					  if(!p.id && !p['account']){
						  layer.msg('请输入账号', {time: 1000})
					  }else if(!p['username']){
						  layer.msg('请输入名称', {time: 1000})
					  }else{
						  console.log(index)
						  partnerlist.addOrUpdateManager(p,index);
					  }
				  }
				  ,btn2: function(index, layero){
				    //按钮【按钮二】的回调
				    
				    //return false 开启该代码可禁止点击该按钮关闭
				  }
				  ,cancel: function(){ 
				    //右上角关闭回调
				    
				    //return false 开启该代码可禁止点击该按钮关闭
				  }
				});     
				
			});
		},
		
		singleDel:function(ids){
			layer.confirm("确认删除吗?", {
				  btn: ["确定","取消"] //按钮
				}, function(){
					partnermanager.dels({"ids":ids},function(){
						layer.msg("成功", {icon: 1});
						partnerlist.getDatalist(0);
					});
				});
		},
		
		addOrUpdateManager : function(p,index){
			partnermanager.addOrUpdateUser(p, function(data){
				if(data && data.retCode == 0){
					layer.msg('操作成功');
					layer.close(index);
				}else{
					layer.msg(data.message || "系统异常", {time: 2000});
				}
			});
		}
};

